<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>

    <meta charset="utf-8">

    <title>未知人脸信息管理</title>
    <!--引入相关的js文件-->
    <link href="../backPage/ui/bootstrap-3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="../backPage/ui/bootstrap-table-1.11.0/bootstrap-table.min.css" rel="stylesheet"/>
    <link href="../backPage/ui/waves-0.7.5/waves.css" rel="stylesheet"/>
    <link href="../backPage/ui/jquery-confirm/jquery-confirm.min.css" rel="stylesheet"/>
    <link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>

    <script src="../backPage/ui/js/jquery-3.3.1.min.js"></script>
    <script src="../backPage/ui/waves-0.7.5/waves.js"></script>
    <script src="../backPage/ui/jquery-confirm/jquery-confirm.min.js"></script>
    <script src="../backPage/ui/bootstrap-3.3.0/js/bootstrap.min.js"></script>
    <script src="../backPage/ui/bootstrap-table-1.11.0/bootstrap-table.min.js"></script>
</head>

<body>
<div id="main">
    <div id="toolbar">
        <button class="btn btn-default" href="javascript:;" onclick="updateAction()">处理人脸信息</button>
        <button th:if="${session.type<=2}"class="btn btn-default" href="javascript:;" onclick="deleteAction()">删除人脸信息</button>
        <br/>
    </div>
    <table id="table"></table>
</div>

<script>
    // 初始化input特效
    function initMaterialInput() {
        $('form input[type="text"]').each(function () {
            if ($(this).val() != '') {
                $(this).parent().find('label').addClass('active');
            }
        });
    }

    var $table = $('#table');


    $(function() {
        //init();

        //更新专业信息
        $("#updateSubmitButton").click(function(){
            var unknownfaceid = $("#unknownFaceId").val();
            var courseId = $("#courseId").val();
            var sid = $("#sid").val();
            var imgPath = $("#imgPath").val();
            if(sid == ''){
                alert("请填写学号信息");
                return;
            }

            if(sid != ''){
                $.ajax({
                    url:"/studentManageSystem/checkSidMatchCid",
                    type:"post",
                    data:{courseId:courseId,sid:sid,imgPath:imgPath},
                    success:function(data){
                        data = JSON.stringify(data);
                        if(data.indexOf("/")!=-1){
                            $.ajax({
                                url:"/studentManageSystem/modifyUnknownFaceInfo",
                                type:"post",
                                data:{unknownfaceid:unknownfaceid,imgPath:imgPath,sid:sid},
                                success:function(res){
                                    //补充
                                    if(res == "OK"){
                                        alert("处理成功");
                                        $("#updateModal").modal('hide');
                                        $table.bootstrapTable('refresh');
                                    }else if(res == ''){
                                        alert("处理失败");
                                        $table.bootstrapTable('refresh');
                                    }
                                }
                            });
                        }else{
                            alert("处理失败");
                        }
                    }
                });
            }
        });

        // bootstrap table初始化
        $table.bootstrapTable({
            url: '/studentManageSystem/getUnKnownFaces',
            height: getHeight(),
            striped: true,
            search: true,
            showRefresh: true,
            showColumns: true,
            minimumCountColumns: 2,
            clickToSelect: true,
            detailView: true,
            pagination: true,
            paginationLoop: false,
            sidePagination: 'server',
            silentSort: false,
            sortOrder: "desc",
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            idField: 'unknownfaceid',
            maintainSelected: true,
            toolbar: '#toolbar',
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'unknownfaceid', title: '未知人脸编号', align: 'center'},
                {field: 'courseid', title: '课程编号',align: 'center'},
                {field: 'createtime', title: '创建时间',align: 'center'},
                {field: 'imgpath', title: '图片路径',align: 'center'},
                {field: 'checkstate', title: '处理状态',align: 'center'},
                {field: 'sid', title: '学号',align: 'center'},
                {field: 'sname', title: '姓名',align: 'center'},
                {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false}
            ]
        });
    });

    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        return [
            '<a class="update" href="javascript:;" onclick="updateAction()" data-toggle="tooltip" title="编辑"><i class="glyphicon glyphicon-edit"></i></a>　',
            '<a class="delete" href="javascript:;" onclick="deleteAction()" data-toggle="tooltip" title="删除"><i class="glyphicon glyphicon-remove"></i></a>'
        ].join('');
    }
    // 格式化时间
    function timeFormatter(value , row, index) {
        return new Date(value).toLocaleString().split(' ')[0];
    }

    function getHeight() {
        return $(window).height() - 20;
    }

    // 删除
    var deleteDialog;
    function deleteAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.confirm({
                title: false,
                content: '请至少选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else {
            deleteDialog = $.confirm({
                type: 'red',
                animationSpeed: 300,
                title: false,
                content: '确认删除该人脸信息吗？',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'waves-effect waves-button',
                        action: function () {
                            var ids = new Array();
                            for (var i in rows) {
                                ids.push(rows[i].unknownfaceid);
                            }
                            $.ajax({
                                type: 'get',
                                url: '/studentManageSystem/deleteUnknownFacesInfo/'+ids.join("_"),
                                success: function(result) {
                                    if(result == "OK"){
                                        deleteDialog.close();
                                        $table.bootstrapTable('refresh');
                                        alert("删除成功");
                                    }else{
                                        alert("删除失败");
                                    }
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                    $.confirm({
                                        theme: 'dark',
                                        animation: 'rotateX',
                                        closeAnimation: 'rotateX',
                                        title: false,
                                        content: textStatus,
                                        buttons: {
                                            confirm: {
                                                text: '确认',
                                                btnClass: 'waves-effect waves-button waves-light'
                                            }
                                        }
                                    });
                                }
                            });
                        }
                    },
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        }
    }

    //创建专业信息
    function createAction(){
        $("#createModal").modal("show");
    }

    //创建专业信息
    function uploadAction(){
        $("#uploadModal").modal('show');
    }

    //编辑专业信息
    function updateAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.confirm({
                title: false,
                content: '请至少选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else {
            //赋初值
            var obj = rows[0];
            $("#unknownFaceId").val(obj['unknownfaceid']);
            $("#faceImg").attr("src",obj['imgpath']);
            $("#courseId").val(obj['courseid']);
            $("#imgPath").val(obj['imgpath']);
            $("#updateModal").modal('show');
        }
    }

</script>

<!-- 处理未知人脸信息模态框（Modal） -->
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="updateModalTitle">
                    处理未知人脸信息
                </h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="unknownFaceId">人脸编号</label>
                        <input type="text" class="form-control" id="unknownFaceId" name="unknownFaceId" placeholder="unknownFaceId" readonly="readonly"/>
                    </div>
                    <div class="form-group">
                        <label for="faceImg">班级图像</label>
                        <img class="form-control" style="height:470px;" id="faceImg" name="faceImg" placeholder="faceImg"/>
                        <input type="hidden" id="imgPath"/>
                    </div>
                    <div class="form-group">
                        <label for="courseId">课程编号</label>
                        <input type="text" class="form-control" id="courseId" name="courseId" placeholder="courseId" readonly="readonly"/>
                    </div>
                    <div class="form-group">
                        <label for="sid">学号</label>
                        <input type="text" class="form-control" id="sid" name="sid" placeholder="sid"/>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="updateSubmitButton">
                    确认修改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

</body>
</html>